import React, { Component } from 'react'
import style from './index.module.scss'
import { Card, Form, Input, Checkbox, Button, message } from 'antd'
import logo from 'assets/logo.png'
import { login } from 'api/user'
import { setToken } from 'utils/storage'
export default class index extends Component {
  state = {
    loading: false,
  }
  render() {
    return (
      <div className={style.login}>
        <Card className="login-container">
          <img src={logo} alt="" className="login-logo " />
          {/* 表单 */}
          <Form
            name="basic"
            size="large"
            onFinish={this.onFinish}
            initialValues={{
              agree: true,
              mobile: '13911111111',
              code: '246810',
            }}
          >
            <Form.Item
              name="mobile"
              validateTrigger={['onChange', 'onBlur']}
              rules={[
                { required: true, message: '手机号不能为空' },
                { pattern: /^1[3-9]\d{9}$/, message: '手机号码格式不对' },
              ]}
            >
              <Input placeholder="请输入你的手机号" autoCapitalize="off" />
            </Form.Item>

            <Form.Item
              name="code"
              rules={[
                { required: true, message: '验证码不能为空' },
                { pattern: /^\d{6}$/, message: '验证码格式不对' },
              ]}
            >
              <Input placeholder="请输入验证码" />
            </Form.Item>

            <Form.Item
              name="agree"
              valuePropName="checked"
              rules={[
                {
                  validator(rule, value) {
                    if (value) {
                      return Promise.resolve()
                    } else {
                      return Promise.reject(new Error('请阅读并同意用户协议'))
                    }
                  },
                },
              ]}
            >
              <Checkbox>我已阅读并同意[用户协议]和[隐私条款]</Checkbox>
            </Form.Item>

            <Form.Item>
              <Button
                type="primary"
                htmlType="submit"
                block
                loading={this.state.loading}
              >
                登录
              </Button>
            </Form.Item>
          </Form>
        </Card>
      </div>
    )
  }
  onFinish = async (valus) => {
    this.setState({ loading: true })
    const { mobile, code } = valus
    try {
      const res = await login(mobile, code)
      // console.log(res)
      message.success('登录成功', 1, () => {
        //存储token
        // localStorage.setItem('geek_pc_token', res.data.token)
        setToken(res.data.token)
        //跳转首页
        // console.log(this.props)
        const { state } = this.props.location
        // console.log(state, '1111')
        if (state) {
          this.props.history.push(state.from.pathname)
        } else {
          this.props.history.push('/home')
        }
      })
    } catch (err) {
      message.warning(err.response.data.message, 1, () => {
        this.setState({ loading: false })
      })
      // console.log(err)
      // message.warning('登录失败')
    }
  }
}
